<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>WebSocket 消息通信</title>
    <style>
        #content {
            width: 500px;
            height: 300px;
            border: 1px solid #ccc;
            padding: 10px;
            overflow-y: auto;
            font-family: Arial, sans-serif;
        }
        .message {
            margin: 5px 0;
            padding: 8px;
            border-radius: 5px;
            max-width: 70%;
            word-wrap: break-word;
        }
        .sent {
            background: #e3f2fd;
            margin-left: auto;
            text-align: right;
        }
        .received {
            background: #f1f1f1;
            margin-right: auto;
            text-align: left;
        }
        #text {
            width: 400px;
            padding: 8px;
        }
        #button {
            padding: 8px 15px;
            background: #4CAF50;
            color: white;
            border: none;
            cursor: pointer;
        }
    </style>
</head>
<body>
<div id="content"></div>
<input type="text" id="text" placeholder="输入要发送的消息">
<input type="button" id="button" value="发送">
</body>
</html>
<script>
    // 随机生成用户ID (1-10000)
    function generateRandomId() {
        return Math.floor(Math.random() * 10000) + 1;
    }
    const channels = ["pc", "Android", "ios"];

    // 从数组中随机选择一个channel
    function getRandomChannel() {
        return channels[Math.floor(Math.random() * channels.length)];
    }

    let socket;
    const contentDiv = document.getElementById('content');

    // 在content div中追加消息
    function appendMessage(text, isSent) {
        const messageDiv = document.createElement('div');
        messageDiv.className = `message ${isSent ? 'sent' : 'received'}`;
        messageDiv.textContent = text;
        contentDiv.appendChild(messageDiv);
        contentDiv.scrollTop = contentDiv.scrollHeight; // 自动滚动到底部
    }

    // 建立WebSocket连接
    function connectWebSocket() {
        const userId = generateRandomId();
        const channel = getRandomChannel();

        // 构建带参数的WebSocket URL
        const wsUrl = `ws://localhost:8080/test_path/websocket/${userId}/${channel}`;
        console.log(`连接参数: userId=${userId}, channel=${channel}`);
        appendMessage(`系统: 连接建立中 (用户ID: ${userId}, 设备: ${channel})`, false);

        socket = new WebSocket(wsUrl);

        socket.onopen = () => {
            appendMessage('系统: WebSocket连接已建立', false);
        };

        socket.onmessage = (event) => {
            appendMessage(`服务器: ${event.data}`, false);
        };

        socket.onerror = (error) => {
            appendMessage(`系统错误: ${error.message}`, false);
        };

        socket.onclose = () => {
            appendMessage('系统: 连接已关闭', false);
        };
    }

    // 发送消息函数
    function sendMessage() {
        const message = document.getElementById('text').value.trim();
        if (!message) {
            alert('请输入要发送的消息');
            return;
        }

        if (socket && socket.readyState === WebSocket.OPEN) {
            socket.send(message);
            appendMessage(`我: ${message}`, true);
            document.getElementById('text').value = '';
        } else {
            appendMessage('系统: 连接未准备好，请稍后再试', false);
        }
    }

    // 页面初始化
    window.onload = function() {
        connectWebSocket();

        // 按钮点击事件
        document.getElementById('button').addEventListener('click', sendMessage);

        // 回车键发送
        document.getElementById('text').addEventListener('keypress', function(e) {
            if (e.key === 'Enter') {
                sendMessage();
            }
        });
    };
</script>